﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DirectoryProvider
{
	
	//[System.Diagnostics.DebuggerStepThroughAttribute()]
	public class DirectoryUser
	{
		
		public string Logon = "";
		public string GivenName = "";
		public string SurName = "";
		public string EmployeeID = "";
		public string[] Mail = {};
		public string Location = "";
		public string Country = "";
		public string[] Groups = {};
		public string Department = "";
		public string DisplayName = "";
		Directory Directory = null;
		public string DN = null;
		System.DirectoryServices.DirectoryEntry _Entry = null;
		
		public DirectoryUser (Directory directory)
		{
			Directory = directory;
		}
		
		public DirectoryUser ()
		{
			Directory = null;
		}
		
		static bool isEmpty (object o)
		{
			if (o == null)
				return true;
			string s = o as string;
			string[] ss = o as string[];
			if (s!=null && s=="")
				return true;
			if (ss != null && (ss.Length == 0 || (ss.Length==1 && string.IsNullOrEmpty(ss[0]))))
				return true;
			return false;
		}
		
		public DirectoryUser Create (string DistinguishedName, object Hash)
		{
			
			if (string.IsNullOrEmpty (DistinguishedName))
				return null;
			
			_Entry = Directory.NewEntry (DN = DistinguishedName);
			
			
			_Entry.PropertyValues ("objectClass").AddRange (Directory.UserObjectClassValues);
			
		
			var hash = Hash.ToStringHash();
			
			foreach (var kvp in hash) {
				var key = Directory.Map [kvp.Key];
				if (string.Compare (key, "objectclass", StringComparison.InvariantCultureIgnoreCase) != 0) {
					if (isEmpty (kvp.Value))
						continue;
					
					_Entry.PropertyValues (key).AddRange (kvp.Value);
				}
			}
			
			
			if (Directory.UserObjectValid (_Entry)) {
				_Entry.CommitChanges ();
				return this;
			} else {
				throw new ArgumentException("Parámetros incorrectos");
				//return null;
			}
		
		}

		
		
		
		/*
		Dictionary<string, string[]> _properties = new Dictionary<string, string[]> ();
		Dictionary<string, string[]> _modified_properties = new Dictionary<string, string[]> ();
		List<string> _deleted_properties = new List<string> ();
		
		internal System.DirectoryServices.DirectoryEntry Entry {
			[System.Diagnostics.DebuggerStepThrough]
			get {
				if (_Entry == null) {
				}
				return _Entry;
				//throw new Exception("no existe entrada");
			}
		}

		public DirectoryUser ()
            : this(null)
		{
		}

		public DirectoryUser (string distinguishedName)
		{
			DN = distinguishedName;
		}

		public static DirectoryUser Create (object hash)
		{

			var u = new DirectoryUser ();


			foreach (var kvp in hash.ToStringHash()) {
				u [kvp.Key] = kvp.Value;
			}


			return u;
		}

		public void Clear ()
		{
			_properties.Clear ();
			_modified_properties.Clear ();
			_deleted_properties.Clear ();
		}

		public void RollBack ()
		{
			_modified_properties.Clear ();
			_deleted_properties.Clear ();
		}

		public bool Commit ()
		{
			if (Entry == null)
				return false;
			return false;
		}

		public IEnumerable<string> this [string idx] {
			get {
				idx = idx.ToLower ();
				if (!_deleted_properties.Contains (idx)) {
					if (_modified_properties.ContainsKey (idx))
						return _modified_properties [idx];
					if (_properties.ContainsKey (idx))
						return _properties [idx];
				}
				return new string[0];
			}

			set {
				idx = idx.ToLower ();


				if (_deleted_properties.Contains (idx)) { // ya está borrado
					if (value == null)
						return; // borrar un borrado
					_deleted_properties.Remove (idx); // quito de la lista de borrados
				}


				if (value == null) {
					if (_modified_properties.ContainsKey (idx))
						_modified_properties.Remove (idx);
					return;
				}

				_modified_properties [idx] = value.ToArray ();
			}
		}
		 */
		
		
		
		
		
		
		
	}
}
